#include <iostream>
#include <string.h>
#include <math.h>
using namespace std;



int main(){
	int n,m,ans=0;
	cin>>n>>m;
	int a[n][m],dp[n][m];
	memset(dp,0,sizeof(dp));
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			cin>>a[i][j];
		}
	}
	for(int i=0;i<n;i++){
		for(int j=0;j<m;j++){
			if(i > 0  &&  j > 0  &&  a[i][j] == a[i-1][j-1]  &&  a[i][j] == 1){
				int num=dp[i-1][j-1];
				int size=1;
				for(int k=1;k<=num;k++){
					if(a[i-k][j] != 1  ||  a[i][j-k] != 1){
						break;
					}
					size++;
				}
				dp[i][j] = size;
			}
			else{
				if(a[i][j] == 1)
					dp[i][j] = 1;
			}
			ans = max(ans,dp[i][j]);
		}
	}
	cout<<ans;
	return 0;
}
